//
//  JCKNoHearViewController.m
//  Builder
//
//  Created by JCK on 2017/12/20.
//  Copyright © 2017年 JCK. All rights reserved.
//

#import "JCKNoHearViewController.h"

#import <WebKit/WebKit.h>
#import "UserInfoModel.h"
#import "SVProgressHUD.h"
#import "BaseSocketManager.h"
@interface JCKNoHearViewController () <WKNavigationDelegate,UICollectionViewDelegate,UINavigationControllerDelegate>


@end

@implementation JCKNoHearViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (!_ishidden)
    {
        [self.navigationController setNavigationBarHidden:YES];
    }
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [SVProgressHUD showWithStatus:@"加载中..."];
    WKWebView *weWebView;
    if (_ishidden)
    {
        weWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    }
    else
    {
        if ([[UserInfoModel shareInstance].equipment isEqualToString:@"iPhone10,3"])
        {
            weWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, -44, ScreenWidth, ScreenHeight+44)];
        }
        else
        {
            weWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
            
        }
    }


    weWebView.navigationDelegate = self;
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:_url]];
    [weWebView loadRequest:request];
    [self.view addSubview:weWebView];
}
#pragma mark - WebKitDelegate
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
    NSString *path = [[navigationAction.request URL] absoluteString];
    
    path = [path stringByRemovingPercentEncoding];
    NSLog(@"urlString=%@",path);
    if ([path rangeOfString:@"protocol://GoToIndex"].location != NSNotFound)
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    decisionHandler(WKNavigationActionPolicyAllow);
}

// 页面开始加载时调用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation
{
    
}
// 当内容开始返回时调用
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation
{

    [SVProgressHUD dismiss];
    
}
// 页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
    [SVProgressHUD dismiss];
}
// 页面加载失败时调用
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation
{
    [self.navigationController setNavigationBarHidden:NO];
    ErrorShow(@"加载失败");
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

